﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Net4.Common.Entities
{
    public class Attachment
    {
        #region Properties

        protected int _Id;
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }

        protected int _ParentId;
        public int ParentId
        {
            get { return _ParentId; }
            set { _ParentId = value; }
        }

        protected string _FileName;
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        protected string _Extension;
        public string Extension
        {
            get { return _Extension; }
            set { _Extension = value; }
        }

        /// <summary>
        /// Gets the virtual path.
        /// </summary>
        /// <value>The virtual path.</value>
        public string VirtualPath
        {
            get 
            {
                return AttachmentManager.Instance.GetRelativeStorageLocation(_FileName);
            }
        }

        protected long _Size;
        public long Size
        {
            get { return _Size; }
            set { _Size = value; }
        }

        public string DisplaySize
        {
            get
            {
                double s = _Size;
                if (s < 1024)
                {
                    return s.ToString("f0") + "bytes";
                }
                s = s / 1024;
                if (s < 1024)
                {
                    return s.ToString("f0") + "KB";
                }
                s = s / 1024;
                return s.ToString("f0") + "MB";
            }
        }

        #endregion

        public Attachment()
        {
        }

        public Attachment(int id, int parentId, string filename, string extension, int size)
        {
            _Id = id;
            _ParentId = parentId;
            _FileName = filename;
            _Extension = extension;
            _Size = size;
        }

        public Attachment(string filename, int parentId)
        {
            FileInfo fileData =
                new FileInfo(AttachmentManager.Instance.GetPhysicalStorageLocation(filename));
            _FileName = fileData.Name;
            _Extension = fileData.Extension;
            _Size = fileData.Length;

            _ParentId = parentId;
        }
    }
}
